<?php

namespace Helper\Model;

use Common\Model\BaseModel;
use Common\Model\ModelException;
use Util\StringUtils;
use Ruben\Aliyun\Mts;
use Think\Log;

class MediaModel extends BaseModel
{
    protected $_auto = [
        ['create_time', 'time', self::MODEL_INSERT, 'function'],
    ];

    protected $_validate = [
    ];

    public function fill($mediaId)
    {
        Log::write("提取封面：==============".$mediaId);
        $config = C('sdk.oss');
        $mts = new Mts($config);
        $mediaInfo = $mts->queryMediaList($mediaId);
        $mediaInfo = $mediaInfo->MediaList->Media[0];
        $url = $mediaInfo->File->URL;

        $media = D('Media')->getByUrl($url);
        if (empty($media)) {
            D('Media')->add([
                'cover_url' => $mediaInfo->CoverURL,
                'height' => $mediaInfo->Height,
                'width' => $mediaInfo->Width,
                'media_id' => $mediaInfo->MediaId,
                'fps' => $mediaInfo->Fps,
                'size' => $mediaInfo->Size,
                'duration' => $mediaInfo->Duration,
                'url' => $url,
                'trans_url' => str_replace("{$config['bucket']}.{$config['endpoint']}", $config['output_cdn'], $url),
                'create_time' => time(),
            ]);  
        } else {
            // $media = [
            //     'cover_url' => $mediaInfo->CoverURL,
            //     // 'url' => $url,
            //     'height' => $mediaInfo->Height,
            //     'width' => $mediaInfo->Width,
            //     'media_id' => $mediaInfo->MediaId,
            //     'fps' => $mediaInfo->Fps,
            //     'size' => $mediaInfo->Size,
            //     'duration' => $mediaInfo->Duration,
            // ];
            D('Media')->where(['url' => $url])->setField([
                'cover_url' => $mediaInfo->CoverURL,
                'height' => $mediaInfo->Height,
                'width' => $mediaInfo->Width,
                'media_id' => $mediaInfo->MediaId,
                'fps' => $mediaInfo->Fps,
                'size' => $mediaInfo->Size,
                'duration' => $mediaInfo->Duration,
            ]);
        }

    }


}
